package cn.flyboy.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.flyboy.entity.*;
import cn.flyboy.service.ExamService;
import cn.flyboy.service.InfoService;
import cn.flyboy.service.StudentService;
import cn.flyboy.utils.IPUtils;
import cn.flyboy.utils.PagingSize;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentController {
    @Resource
    private StudentService studentServiceImpl;
    @Resource
    private ExamService examServiceImpl;
    @Resource
    private InfoService infoServiceImpl;

    private static String stu_ip; // 用户ip地址

    @RequestMapping("studentLogin")
    public String login(Student student, HttpServletRequest request, HttpSession session) {
        System.out.println(student.getStu_id() + "**************" + student.getStu_name());
        boolean isIpAddress = IPUtils.isIpAddress(student.getStu_name());
        stu_ip = student.getStu_name();
        System.out.println(stu_ip + "++++++++++++++++----------------");
        if (isIpAddress == false) {
            Student stu = studentServiceImpl.login(student);
            if (stu != null) {
                if (stu.getStu_ip() == null || stu.getStu_ip().equals("")) {
                    stu.setStu_ip(IPUtils.getRemoteHost(request));
                    int index = studentServiceImpl.updStuIp(stu);
                    if (index < 1) {
                        session.setAttribute("info", "<script>alert('ip地址获取有误');</script>");
                        return "redirect:/success/loginError.jsp";
                    }
                } else if (stu.getStu_ip().equals(IPUtils.getRemoteHost(request))) {
                    session.setAttribute("info", "<script>alert('您的ip地址已被绑定，请使用绑定的ip登录，或联系管理员');</script>");
                    return "redirect:/success/loginError.jsp";
                }

                session.setAttribute("student", stu);
                String StartExamTeaName = examServiceImpl.selectStartExamTeaName(stu.getStu_exam());
                System.out.println("+++++++++++++++" + StartExamTeaName);
                session.setAttribute("StartExamTeaName", StartExamTeaName);
                session.setAttribute("info", "登录成功");
                return "redirect:/success/studentSuc.jsp";
            } else {
                session.setAttribute("info", "登录失败");
                return "redirect:/success/loginError.jsp";
            }
        } else {
            String stu_id = student.getStu_id();

            Student stu = studentServiceImpl.selByStudentIP(stu_id, stu_ip);
            if (stu != null) {
                session.setAttribute("student", stu);
                String StartExamTeaName = examServiceImpl.selectStartExamTeaName(stu.getStu_exam());
                System.out.println("+++++++++++++++" + StartExamTeaName);
                session.setAttribute("StartExamTeaName", StartExamTeaName);
                session.setAttribute("info", "登录成功");
                return "redirect:/success/studentSuc.jsp";
            } else {
                session.setAttribute("info", "登录失败");
                return "redirect:/success/loginError.jsp";
            }
        }
    }

    @RequestMapping(value = "getAllInfoOfStuAns", method = {RequestMethod.PATCH, RequestMethod.GET})
    @ResponseBody
    public Msg getAllAnswerInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, String stu_id, String stu_exam, HttpSession session, HttpServletRequest request) {
        int spiltPageCount = PagingSize.splitPageSize(request);
        // 查询之前调用startPage，传入分码，以及分页大小
        PageHelper.startPage(pageNum, spiltPageCount);
        // startPage紧跟的查询就是分页查询
        List<Answer> lists = studentServiceImpl.selAllAnswerInfo(stu_id, stu_exam);
        // 使用pageInfo包装查询后的结果，只需要将pageInfo交给页面就行了。
        // 封装了详细的分页信息,包括有我们查询出来的数据，传入连续显示的页数
        PageInfo<Answer> page = new PageInfo<Answer>(lists, StaticResources.navigatePages);
        session.setAttribute("pageInfo", page);
        System.out.println("总页码：" + page.getPages());
        System.out.println("总记录数：" + page.getTotal());
        return Msg.success().add("pageInfo", page);
    }

    @RequestMapping(value = "getExamStudents", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Msg getExamStudents(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, String e_name, String e_teacher, HttpServletRequest request) {

        System.out.println(e_name + "****************" + e_teacher);
        int spiltPageCount = PagingSize.splitPageSize(request);
        // 查询之前调用startPage，传入分码，以及分页大小
        PageHelper.startPage(pageNum, spiltPageCount);
        // startPage紧跟的查询就是分页查询
        List<Student> lists = studentServiceImpl.selAllStudents(e_name, e_teacher);

        // 使用pageInfo包装查询后的结果，只需要将pageInfo交给页面就行了。
        // 封装了详细的分页信息,包括有我们查询出来的数据，传入连续显示的页数
        PageInfo<Student> page = new PageInfo<Student>(lists, StaticResources.navigatePages);
        System.out.println("总页码：" + page.getPages());
        System.out.println("总记录数：" + page.getTotal());
        return Msg.success().add("pageInfo", page);
    }

    @RequestMapping(value = "getExamStudentNums", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Student> getExamStudentNums(String e_name, String e_teacher) {
        return studentServiceImpl.selAllStudents(e_name, e_teacher);
    }

    @RequestMapping("unbind")
    @ResponseBody
    public void unbind(String stu_id) {
        int index = studentServiceImpl.updUnBind(stu_id);
        if (index >= 1) {
            System.out.println("IP解绑成功");
        } else {
            System.out.println("IP解绑失败");
        }
    }

    @RequestMapping(value = "selStudentById", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Msg selStudentById(@RequestParam(value = "pageNum", defaultValue = "5") Integer pageNum, String stu_id, HttpSession session, HttpServletRequest request) {
        System.out.println("/*/*/*/*/*/*/*" + stu_id + "+++++" + pageNum);

        Object obj = session.getAttribute("examStartName");
        if (obj != null) {
            int spiltPageCount = PagingSize.splitPageSize(request);
            // 查询之前调用startPage，传入分码，以及分页大小
            PageHelper.startPage(pageNum, spiltPageCount);
            // startPage紧跟的查询就是分页查询
            List<Student> lists = studentServiceImpl.selStudentById(stu_id, obj.toString());

            // 使用pageInfo包装查询后的结果，只需要将pageInfo交给页面就行了。
            // 封装了详细的分页信息,包括有我们查询出来的数据，传入连续显示的页数
            PageInfo<Student> pages = new PageInfo<Student>(lists, StaticResources.navigatePages);
            session.setAttribute("pageInfo", pages);
            System.out.println("总页码：" + pages.getPages());
            System.out.println("总记录数：" + pages.getTotal());
            return Msg.success().add("pageInfo", pages);
        }
        String e_name = examServiceImpl.selStartExam().get(0).getE_name();
        session.setAttribute("examStartName", e_name);
        // 查询之前调用startPage，传入分码，以及分页大小
        int spiltPageCount = PagingSize.splitPageSize(request);
        PageHelper.startPage(pageNum, spiltPageCount);
        // startPage紧跟的查询就是分页查询
        List<Student> lists = studentServiceImpl.selStudentById(stu_id, e_name);

        // 使用pageInfo包装查询后的结果，只需要将pageInfo交给页面就行了。
        // 封装了详细的分页信息,包括有我们查询出来的数据，传入连续显示的页数
        PageInfo<Student> pages = new PageInfo<Student>(lists, StaticResources.navigatePages);
        session.setAttribute("pageInfo", pages);
        System.out.println("总页码：" + pages.getPages());
        System.out.println("总记录数：" + pages.getTotal());
        return Msg.success().add("pageInfo", pages);
    }


    @RequestMapping(value = "studentInformation", method = {RequestMethod.GET, RequestMethod.POST})
    public String getAllInfos(String exam, String tName, HttpSession session) {
        System.out.println(exam + "   +++++++++++   " + tName);
        List<Information> lists = infoServiceImpl.getAllExamInfo(exam, tName);
        session.setAttribute("lists", lists);
        return "redirect:/student/student_notify.jsp";
    }


    @RequestMapping("exit")
    public String exit(HttpSession session) {
        session.removeAttribute("student");
        session.removeAttribute("teacher");
        return "redirect:/home.jsp";
    }
}
